/****************************************************************
 * File:	hal_timer.h
 * Description:	Timer2(MAC Timer) operations for CC2430
 *
 * Date: 2010-05-22
 ***************************************************************/

#ifndef HAL_TIMER_H
#define HAL_TIMER_H
#include "hal.h"

/**********************************************************
 	**	External Functions 	**
 *********************************************************/

extern void mac_timer_init();
extern UINT32 mac_timer_get();
extern UINT32 mac_ticks_to_us(UINT32 ticks);
extern void mac_delay_time_ms(UINT32 delay_ms);

/**********************************************************
 	**	External Variables		**
 *********************************************************/
extern UINT32 volatile jiffies; 	// jiffies update every 100ms

/**********************************************************
 	**	Avoid Time Wrap Macro	**
 *********************************************************/
#define mac_time_after(unknown, known) ((INT32)(known)-(INT32)(unknown) < 0)
#define mac_time_before(unknown, known) ((INT32)(unknown)-(INT32)(known) < 0)
#define mac_time_after_eq(unknown, known) ((INT32)(unknown)-(INT32)(known) >= 0)
#define mac_time_before_eq(unknown, known) ((INT32)(known)-(INT32)(unknown) >= 0)


#define HZ 10		// interrupt every 100ms
// assuming 16us period, have 1/16us = 62500 tics per seocnd
#define T2CMPVAL (62500/HZ)

#define LRWPAN_SYMBOLS_PER_SECOND 62500
//Timer Support
//For the CC2430, we set the timer to exactly 1 tick per symbol
//assuming a clock frequency of 32MHZ, and 2.4GHz
#define SYMBOLS_PER_MAC_TICK()	1
#define SYMBOLS_TO_MACTICKS(x)	(x/SYMBOLS_PER_MAC_TICK())	//every
#define MSECS_TO_MACTICKS(x)	(x*(LRWPAN_SYMBOLS_PER_SECOND/1000))
#define MACTIMER_MAX_VALUE	0x000FFFFF	//20 bit counter, about 16 seconds max for 32MHZ crystal
#define MAC_TIMER_NOW_DELTA(x)	((mac_timer_get() - (x)) & MACTIMER_MAX_VALUE)

#endif

